package com.cn.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.exception.MyException;

import java.util.List;

public class FastJsonUtil {

    /**
     * 字符串  转 list
     *
     * @param txt
     * @param cls
     * @return
     */
    public static List str2List(Object txt, Class cls) {
        if (txt == null) throw new MyException("字符串不能为空");
        return JSON.parseArray(txt + "", cls);
    }

    /**
     * 对象转 json
     *
     * @param obj
     * @return
     */
    public static JSONObject obj2Json(Object obj) {
        return (JSONObject) JSON.toJSON(obj);
    }

    /**
     * 对象转json字符串
     *
     * @param obj
     * @return
     */
    public static String obj2Jsonstr(Object obj) {
        if (obj == null) {
            return null;
        }
        return JSON.toJSON(obj).toString();
    }

}
